﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using Editor.PageControls;
using Editor.Utilities;

namespace Editor
{
    public partial class LabelProperties : PropertiesFormBase, IDictSubject<string>
    {
        public LabelProperties()
        {
            InitializeComponent();
        }

        protected override void PropertiesForm_Load(object sender, EventArgs e)
        {
            base.PropertiesForm_Load(sender, e);

            PageLabel selectedLabel = PageControl.SelectedControl as PageLabel;

            ControlName = selectedLabel.Name;
            textBoxName.Text = selectedLabel.Name;
            textBoxFont.Text = selectedLabel.TextFontSize.ToString();
            panelColor.BackColor = selectedLabel.ForeColor;
            textBoxX.Text = selectedLabel.Location.X.ToString();
            textBoxY.Text = selectedLabel.Location.Y.ToString();
            textBoxMaxWidth.Text = selectedLabel.MaxWidth.ToString();
            Dictionary = selectedLabel.Dictionary;

            if (comboBoxLanguage.SelectedIndex == -1)
                comboBoxLanguage.Items.AddRange(EditorForm.SupportedLanguages);

            resetSubject();
            textBoxText.Text = selectedLabel.Dictionary[EditorForm.CurrentLanguage];
            comboBoxLanguage.SelectedIndex = EditorForm.CurrentLanguageIndex;
        }

        protected override void buttonApply_Click(object sender, EventArgs e)
        {
            PageLabel selectedLabel = PageControl.SelectedControl as PageLabel;

            if (textBoxName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("İsim girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!selectedLabel.Name.Equals(textBoxName.Text.Trim()) &&
                !EditorForm.IsElementNameAvailable(textBoxName.Text.Trim()))
            {
                MessageBox.Show("Bu isim zaten var!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (textBoxText.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Yazı girmelisiniz!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            float fontSize = -1f;

            if (!float.TryParse(textBoxFont.Text, System.Globalization.NumberStyles.Float, CultureInfo.CurrentCulture,
                out fontSize) || fontSize <= 0)
            {
                MessageBox.Show("Font büyüklüğü kesirli pozitif bir sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int maxWidth = -1;

            if (!int.TryParse(textBoxMaxWidth.Text, out maxWidth) || maxWidth <= 0)
            {
                MessageBox.Show("Azami genişlik bir pozitif tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            int x = -1, y = 1;

            if (!int.TryParse(textBoxX.Text, out x) || x < 0)
            {
                MessageBox.Show("X değeri pozitif bir tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            if (!int.TryParse(textBoxY.Text, out y) || y < 0)
            {
                MessageBox.Show("Y değeri pozitif bir tam sayı olmalı!", "Hata!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = System.Windows.Forms.DialogResult.Retry;
                return;
            }

            string textToAdd = textBoxText.Text;

            if (textToAdd != string.Empty && !textToAdd.EndsWith("\n"))
                textToAdd += "\n";

            string selectedLanguage = comboBoxLanguage.SelectedItem as string;

            Dictionary[selectedLanguage] = textToAdd;
            selectedLabel.Dictionary = Dictionary;
            selectedLabel.Name = textBoxName.Text.Trim();
            selectedLabel.ForeColor = panelColor.BackColor;
            selectedLabel.Location = new Point(x, y);
            selectedLabel.TextFontSize = fontSize;
            selectedLabel.MaxWidth = maxWidth;

            base.buttonApply_Click(sender, e);
        }

        private void buttonPick_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                panelColor.BackColor = colorDialog1.Color;
                EditorForm.IsSaved = false;
            }
        }

        protected override void buttonRemove_Click(object sender, EventArgs e)
        {
            base.buttonRemove_Click(sender, e);
        }

        protected override void buttonCopy_Click(object sender, EventArgs e)
        {
            base.buttonCopy_Click(sender, e);
        }

        protected override void buttonRelocate_Click(object sender, EventArgs e)
        {
            base.buttonRelocate_Click(sender, e);
        }

        private void comboBoxLanguage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (textBoxText != null)
            {
                string textToAdd = textBoxText.Text;

                if (textToAdd != string.Empty && !textToAdd.EndsWith("\n"))
                    textToAdd += "\n";

                SDictionary<string> dict = Dictionary;
                textBoxText.Text = changeSubject(comboBoxLanguage.SelectedItem as string,
                    comboBoxLanguage.SelectedIndex, textToAdd, ref dict);
                Dictionary = dict;
            }
        }

        public SDictionary<string> Dictionary { get; set; }
    }
}
